<?php


    class Login extends database_object{
        
        public $id;
        public $email;
        public $password;
        public $activated;
        public $user_id;
        public $activatestring;
        
        protected static $table_name = "bo_login";
        protected static $db_fields = array("id", "email", "password", "activated", "user_id", "activatestring");
        
     
        
        // Method builds a login object
        public function build($loginobject){
            $loginobject->id = $loginobject->id;
            $loginobject->save();
            return $loginobject;
        }
        
        
        //Authenticates the user input. If email and password matches, true. Else false
        public static function auth($email, $password){
            
            $database = Database::getInstance();
            $email = $database->escape_value($email);
            $password = $database->escape_value($password);

            $sql  = "SELECT * FROM bo_login ";
            $sql .= "WHERE email = '{$email}' ";
            $sql .= "AND password = '{$password}' ";
            $sql .= "LIMIT 1";
            $result_array = self::find_by_sql($sql);
                        
            return !empty($result_array) ? array_shift($result_array) : false;
	}
          
        // Authenticates user by password and user_id. True if match, false else.
        public static function authUserId($userid, $password){
            
            $database = Database::getInstance();
            $password = $database->escape_value($password);

            $sql  = "SELECT * FROM bo_login ";
            $sql .= "WHERE user_id = '{$userid}' ";
            $sql .= "AND password = '{$password}' ";
            $sql .= "LIMIT 1";
            $result_array = self::find_by_sql($sql);
                        
            return !empty($result_array) ? array_shift($result_array) : false;
	}
        
        //Method checks if input email exists in the database
        public static function existingEmail($email){
            
            $database   = Database::getInstance();
            $mail       = $database->escape_value($email);
            
            $sql =  "SELECT * FROM bo_login ";
            $sql .= "WHERE email = '".$mail."';";
            $result_array = self::find_by_sql($sql);
                       
            if($result_array!= null){
                 return !empty($result_array) ? array_shift($result_array) : false;
                
            }
            else return false;
           
        }
        
        // Method generates a string used for user activation
        public function createaAString($email,$password){
            $activatestring = sha1($email.'ThisIsFunky'.$password);
            
            return $activatestring;
          }
          
          // Method returns login object from user_id
        public function find_Login_by_userid($userid){
            $database = Database::getInstance();
            
            if($userid != 0){
               
                $sql  = "SELECT * FROM " . self::$table_name;
                $sql .= " WHERE user_id=" .$userid.";";

                return self::find_by_sql($sql);

            
                
                return false;
            }
          }
          
    }
$login = new Login();


?>
